package com.spstudio.iforward.core.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.ConversationScoped;
import javax.inject.Named;

@ConversationScoped
@Named
public class PageObject implements Serializable, Cloneable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -55507559515796L;
	
	private int currentPageNumber = 1; // 当前页序号
	private long totalRecordsNumber = Long.MAX_VALUE; // 记录总数
	private int maxRecordsPerPage = 6; // 每页最大记录条数， 默认每页8条数据
	private List<Integer> listPageNo; // 可点击的页码
	private int maxClickablePageNoPerPage = 5; // 同时显示多少个页码
	private int currentClickablePageNoIndex = 1; // 页码页数
	
	public PageObject clone(){
		PageObject p = null;
		try{
			p = (PageObject)super.clone();
		}catch(CloneNotSupportedException e){
			e.printStackTrace();
		}
		return p;
	}
	
	public void reset(){
		setCurrentPageNumber(1);
		totalRecordsNumber = Long.MAX_VALUE;
	}
	
	public void tuneNextPageGroup(){
		if (currentClickablePageNoIndex * maxClickablePageNoPerPage * maxRecordsPerPage < totalRecordsNumber){
//			currentPageNumber = currentClickablePageNoIndex * maxClickablePageNoPerPage + 1;
//			currentClickablePageNoIndex ++;
			generateListNo();
		}
	}
	
	public void tunePriorPageGroup(){
		if (currentClickablePageNoIndex > 1){
//			currentClickablePageNoIndex --;
//			currentPageNumber = (currentClickablePageNoIndex - 1) * maxClickablePageNoPerPage + 1;
			generateListNo();
		}
	}
	
	public void tuneNextPageGroupBackend(){
		if (currentClickablePageNoIndex * maxClickablePageNoPerPage * maxRecordsPerPage < totalRecordsNumber){
			currentPageNumber = currentClickablePageNoIndex * maxClickablePageNoPerPage + 1;
			currentClickablePageNoIndex ++;
			generateListNo();
		}
	}
	
	public void tunePriorPageGroupBackend(){
		if (currentClickablePageNoIndex > 1){
			currentClickablePageNoIndex --;
			currentPageNumber = (currentClickablePageNoIndex - 1) * maxClickablePageNoPerPage + 1;
			generateListNo();
		}
	}
	
	private void generateListNo(){
		if (listPageNo == null){
			listPageNo = new ArrayList<Integer>();
		}
		listPageNo.clear();
		for (int i = 0; i < maxClickablePageNoPerPage; i ++){
			int newPageNo = (currentClickablePageNoIndex - 1) * maxClickablePageNoPerPage + 1 + i;
			if ((newPageNo - 1) * maxRecordsPerPage >= totalRecordsNumber){
				break;
			}
			listPageNo.add(newPageNo);
		}
	}

	public int getCurrentPageNumber() {
		return currentPageNumber;
	}

	public void setCurrentPageNumber(int currentPageNumber) {
		this.currentPageNumber = currentPageNumber;
		// calculate currentClickablePageNoIndex
		currentClickablePageNoIndex = (currentPageNumber - 1) / maxClickablePageNoPerPage + 1;
	}
	
	public int getMaxPageNumber() {
		return (int)(totalRecordsNumber / maxRecordsPerPage) + (int)(totalRecordsNumber % maxRecordsPerPage == 0 ? 0 : 1);
	}

	public long getTotalRecordsNumber() {
		return totalRecordsNumber;
	}

	public void setTotalRecordsNumber(long totalRecordsNumber) {
		this.totalRecordsNumber = totalRecordsNumber;
	}

	public int getOffset() {
		return (currentPageNumber - 1) * maxRecordsPerPage;
	}

	public int getMaxRecordsPerPage() {
		return maxRecordsPerPage;
	}

	public void setMaxRecordsPerPage(int maxRecordsPerPage) {
		this.maxRecordsPerPage = maxRecordsPerPage;
	}

	public List<Integer> getListPageNo() {
		generateListNo();
		return listPageNo;
	}

	public void setListPageNo(List<Integer> listPageNo) {
		this.listPageNo = listPageNo;
	}
	
	public boolean isAlreadySetTotalRecordsNumber(){
		return totalRecordsNumber != Long.MAX_VALUE;
	}
	public boolean isHasNextPageGroup() {
		return currentClickablePageNoIndex * maxClickablePageNoPerPage * maxRecordsPerPage < getTotalRecordsNumber();
	}
	public boolean isHasPrePageGroup() {
		return currentClickablePageNoIndex > 1;
	}
	
}
